import Database
import datetime

def getNextNumForLocation(cursor, location_id):
    query = "SELECT * FROM artifacts WHERE location_id = '%s' ORDER BY artifact_id DESC LIMIT 1" % location_id
    Database.executeCommand(query, cursor)
    row = cursor.fetchone()
    if row:
        return int(row['location_incrementer']) + 1
    else:
        return 1
    return None

def checkIfNextiExists(cursor,next_i,location_id):
    query = "select * from artifacts where location_id = '%s' and location_incrementer = '%s'" % (location_id, next_i)
    Database.executeCommand(query, cursor)
    result = cursor.fetchone()
    print result
    if result:
        print "Already exists"
        return int(next_i) + 1
    else:
        print "Doesn't exist"
        return next_i

def getRecentFolders(cursor):
    query = "SELECT * FROM locations ORDER BY location_id DESC LIMIT 0, 10"
    Database.executeCommand(query, cursor)
    
    return cursor.fetchall()

def createNewFolder(conn, cursor, room, case_num, shelf_num, box_num, folder_name):
    newLoc = getNextLocation(cursor)
    
    if newLoc == -1:
        #something failed
        return -1
    
    query = "INSERT INTO locations (room, case_num, shelf_num, box_num, folder_name) VALUES('%s', '%s', '%s', '%s', '%s')" % (room, case_num, shelf_num, box_num, folder_name)
    print "Query: " + query
    Database.executeCommand(query, cursor)
    print "going to return LOCATION FROM NEW FOLDER: " + str(newLoc)
    return str(newLoc)
    
def getNextLocation(cursor):
    query = "SELECT * FROM locations ORDER BY location_id DESC LIMIT 1"
    Database.executeCommand(query, cursor)
    
    row = cursor.fetchone()
    if row:
        return int(row['location_id']) + 1
    else:
        return -1
    
def getBaseNameForLocation(cursor, location_id):
    query = "SELECT * FROM locations WHERE location_id = '%s'" % location_id
    Database.executeCommand(query, cursor)
    row = cursor.fetchone()
    if row:
        return "%s%s%s%s_%s" % (row['room'], row['case_num'], row['shelf_num'], row['box_num'], row['folder_name'])
    else:
        return None
    return None

def makeNewArtifact(conn, cursor, location_id, location_incrementer, base_name, added_by, ir_reflectance):
    print "in create artifact with location: " + str(location_id)
    query = "INSERT INTO artifacts (location_id, location_incrementer, base_name, added_by, ir_reflectance, added_datetime) VALUES ('%s', '%s','%s','%s','%s', '%s')" % (location_id, location_incrementer, base_name, added_by, ir_reflectance, datetime.datetime.today())
    Database.executeCommand(query, cursor)
    conn.commit()
    return conn.insert_id()

def addImageToArtifact(conn, cursor, artifact_id, image_name, raw_path, scanned_by, scan_type, added_by):
    query = "INSERT INTO images (artifact_id, image_name, raw_path, scanned_by, scan_type, added_by, scanned_datetime) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (artifact_id, image_name, raw_path, scanned_by, scan_type, added_by, datetime.datetime.today())
    Database.executeCommand(query, cursor)
    conn.commit()
    return conn.insert_id()
    